#ifndef __FRANESOURCE__
#define __FRANESOURCE__

#include <imp/imp_common.h>
#include <imp/imp_system.h>
#include <imp/imp_framesource.h>
#include <system/imp_v4l2.h>
#include <system/system.h>
#include <system/module.h>
#include <system/group.h>
#include <system/device.h>
#include <system/vbm.h>
#include "fisheye_interface.h"

typedef enum {
	FSCHANNEL_STATE_CLOSE,
	FSCHANNEL_STATE_OPEN,
	FSCHANNEL_STATE_RUN,
} FSChannelState;

struct FSDepth {
	IMPFrameInfo *frame;
	struct FSDepth *prev;
	struct FSDepth *next;
};

struct FSChannelDistortionCorrect {
	int en;
	undistort_handle *handle;
	int mode;
	IMPFrameInfo	*framebuf1;
};

struct ExtChannelBuffer
{
	int nr_buffers;
	void *resize_tmp_buf;
	void *resize_frame_buf;
	IMPFrameInfo *frames[0];
};

typedef struct {
	int index;
	int ivdc_fd;
	int direct_mode;
	void *ivdc_alloc_addr;
	int data_threshold;
	FSChannelState state;
	IMPFSChnAttr attr;
	struct frame_image_format drv_fmt;
	struct v4l2_format fmt;
	/* used for distortion correction */
	struct FSChannelDistortionCorrect distortion;

	int fr_size;
	pthread_t tid;
	int fs_fd;
	Group *group;

	int depth;
	pthread_cond_t cond;
	pthread_mutex_t mutex;
	struct FSDepth *list_done;
	struct FSDepth *list_used;
	struct FSDepth *list_prepare;

	int qframecnt;
	int dqframecnt;

	int x_offset;
	int y_offset;

	struct ExtChannelBuffer *ext_buffer;
	sem_t ext_sync_sem;
	void *fifo_frame;
	int fifo_frame_maxcnt;
	int fifo_frame_cnt;
	int fifo_type;
	uint64_t wait_frame_ts;
	int wait_frame_flag;

	pthread_cond_t cond_timedframe;

	//snap one frame
	pthread_cond_t cond_snapframe;
#define STATUS_SNAPFRAME_IDLE (0)
#define STATUS_SNAPFRAME_START (1)
#define STATUS_SNAPFRAME_DONE (2)
	uint32_t status_snapframe;
	uint8_t *pbuf_snapframe;
	uint32_t fmt_snapframe;
	IMPFrameInfo *info_snapframe;
	//used in ext channel reisze
	uint8_t *pbuf_resize;
} FSChannel;

typedef struct {
	Device *device;
	uint32_t inFrmRateNum;		/**< 通道的输入帧率分子；只能获取，不能设置*/
	uint32_t inFrmRateDen;		/**< 通道的输入帧率分母；只能获取，不能设置*/
	uint32_t is_changwait_cnt;
	int32_t FS_EnablePHYChn_Num;
	FSChannelState state;
	FSChannel channel[NR_MAX_FS_CHNS];
} Framesource;

#endif
